<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/** 
* Smarty function plugin (function.fckeditor.php)
* Requires PHP >= 4.3.0
* ------------------------------------------------------------- 
* Type:     function 
* Name:     fckeditor
* Version:  1.0
* Author:   gazoot (gazoot care of gmail dot com)
* Purpose:  Creates a FCKeditor, a very powerful textarea replacement.
* ------------------------------------------------------------- 
* @param InstanceName Editor instance name (form field name)
* @param BasePath optional Path to the FCKeditor directory. Need only be set once on page. Default: /FCKeditor/
* @param Value optional data that control will start with, default is taken from the javascript file
* @param Width optional width (css units)
* @param Height optional height (css units)
* @param ToolbarSet optional what toolbar to use from configuration
* @param CheckBrowser optional check the browser compatibility when rendering the editor
* @param DisplayErrors optional show error messages on errors while rendering the editor
*
* Default values for optional parameters (except BasePath) are taken from fckeditor.js.
*
* All other parameters used in the function will be put into the configuration section, 
* CustomConfigurationsPath is useful for example.
* See http://wiki.fckeditor.net/Developer%27s_Guide/Configuration/Configurations_File for more configuration info.
*/ 
function smarty_function_tinymce($params, &$smarty) {
        
        if(!isset($params['InstanceName']) || empty($params['InstanceName']))
        { 
                $smarty->trigger_error('tinyMCE: required parameter "InstanceName" missing');
        }

        static $base_arguments = array();
        static $config_arguments = array();

        // Test if editor has been loaded before
        if(!count($base_arguments)) 
        	$init = TRUE;
        else 
        	$init = FALSE;
        
		$base_arguments['InstanceName'] = $params['InstanceName'];

        if(isset($params['Value'])) 
        	$base_arguments['Value'] = $params['Value'];
        
        if(isset($params['Width'])) 
        	$base_arguments['Width'] = $params['Width'];
        if(isset($params['Height'])) 
        	$base_arguments['Height'] = $params['Height'];
		
		$base_arguments['Theme'] = 'simple';
		
		if(isset($params['Theme']))
			$base_arguments['Theme'] = $params['Theme'];
			
		if(isset($params['PreviewURI']))
			$base_arguments['PreviewURI'] = $params['PreviewURI'];

		if($base_arguments['Theme'] === 'advanced') {
				$base_arguments['advanced_options'] ='
				plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,ibrowser",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
				theme_advanced_buttons2 : "|,ibrowser,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
				plugin_preview_pageurl: "'.$base_arguments['PreviewURI'].'",
				plugin_preview_width: 800,
    			plugin_preview_height: 600'; 
		} 


//        if(isset($params['ToolbarSet'])) $base_arguments['ToolbarSet'] = $params['ToolbarSet'];
//        if(isset($params['CheckBrowser'])) $base_arguments['CheckBrowser'] = $params['CheckBrowser'];
//        if(isset($params['DisplayErrors'])) $base_arguments['DisplayErrors'] = $params['DisplayErrors'];
//
//        // Use all other parameters for the config array (replace if needed)
//        $other_arguments = array_diff_assoc($params, $base_arguments);
//        $config_arguments = array_merge($config_arguments, $other_arguments);

        $out = '';

        if($init) {
        	
        	$out .= '<script type="text/javascript" src="/js/tiny_mce/tiny_mce.js"></script>
						<script type="text/javascript">
						tinyMCE.init({
							mode: "textareas",
							theme: "'.$base_arguments['Theme'].'",
							width: "'.$base_arguments['Width'].'", 
						    height: "'.$base_arguments['Height'].'",' .
						    $base_arguments['advanced_options'].
				     		'
						});
						</script>';
						
						if($base_arguments['Theme'] === 'advanced')
							$out .= '<textarea id="'.$base_arguments['InstanceName'].'" name="'.$base_arguments['InstanceName'].'">'.$base_arguments['Value'].'</textarea>';
							
				//<script>//tinyMCE.execCommand("mceAddControl", true, "'.$base_arguments['InstanceName'].'");</script>
                //$out .= '<script type="text/javascript" src="' . $base_arguments['BasePath'] . '/fckeditor.js"></script>';
        }

//        $out .= "\n<script type=\"text/javascript\">\n";
//        $out .= "var oFCKeditor = new FCKeditor('" . $base_arguments['InstanceName'] . "');\n";
/*
        foreach($base_arguments as $key => $value)
        {
                if(!is_bool($value))
                {*/
                        // Fix newlines, javascript cannot handle multiple line strings very well.
           /*             $value = '"' . preg_replace("/[\r\n]+/", '" + $0"', addslashes($value)) . '"';
           
                }
                $out .= "oFCKeditor.$key = $value; \n";
        }

        foreach($config_arguments as $key => $value)
        {
                if(!is_bool($value))
                {
                        $value = '"' . preg_replace("/[\r\n]+/", '" + $0"', addslashes($value)) . '"';
                }
                $out .= "oFCKeditor.Config[\"$key\"] = $value; \n";
        }*/
		// (*) CustomConfiguration - get from  BasePath
		/*
		$out .= "oFCKeditor.Config[\"CustomConfigurationsPath\"] = '".$base_arguments['BasePath']."fckconfig.js' ;\n";
        $out .= "oFCKeditor.Create();\n";
        $out .= "</script>\n"; 
        */
        return $out;
} 
 
?>